﻿'use strict';
app.controller('WithdrawController',
    [
    '$rootScope',
    '$scope',
    '$localStorage',
    '$state',
    '$modal',
    '$http',
    '$confirm',
    'commonFactory',
    'PinManageFactory',
function ($rootScope, $scope, $localStorage, $state, $modal, $http,$confirm, commonFactory, PinManageFactory) {
    $scope.title = "GH",
    $scope.messeger = "",
    $scope.params = "",
    $scope.params2 = "",
    $scope.popup = function (windowClass, StyleModal, Controller, size, params, params2) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: StyleModal,
            controller: Controller,
            size: size,
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                },
                params: function () {
                    $scope.params = params;
                    return $scope.params;
                },
                params2: function () {
                    $scope.params2 = params2;
                    return $scope.params2;
                },
            }
        })
    },
    $scope.List = [],
    $scope.doConfirm = function (Id) {
        //2:Confirm
        commonFactory.confirmTransferOrder(Id, 2).then(function (rsp) {
            if (rsp.data.Status) {
                window.location.reload();
            } else {
                $scope.messeger = rsp.data.Msg,
                $scope.title = 'Warning',
                $scope.popup('modal-message modal-warning', "WarningModal");
            }
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    },
    GetListWd($localStorage.user.UserId);
    function GetListWd(UserId)
    {
        commonFactory.getListWd(UserId).then(
         function successCallback(resp) {
             if (resp.data.Status) {
                 $scope.List = resp.data.Result;
             } else
                 $scope.messeger = resp.data.Msg;
         }, function errorCallback(resp) {
             $scope.messeger = "";
         })
    };
}
]);
app.controller('WithdrawModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $http, $rootScope, $timeout, $state, $stateParams, messeger, title, params, params2, commonFactory, WalletFactory) {

    $scope.messeger = "",
    $scope.title = title,
    $scope.processing = false,
    $scope.AlertType = "warning",
    $scope.profit = 0,
    $scope.amount = 1500000,
    GetProfitWallet($localStorage.user.UserId),
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    },
    $scope.doWithdraw = function (data) {
        if (parseFloat($scope.profit) >= parseFloat($scope.amount) && parseFloat($scope.profit) > 0) {
            commonFactory.createWithdraw($localStorage.user.UserId, $scope.amount).then(function (rsp) {
                if (rsp.data.Status) {
                    $scope.processing = true;
                    $scope.messeger = rsp.data.Msg;
                } else {

                }
            });
        } else {
            $scope.processing = false;
            $scope.messeger = "Balance is not enough";
        }
    };
    function GetProfitWallet(userId)
    {
        commonFactory.getWalletById(userId, 2).then(function (rsp) {
            if (rsp.data.Status) {
                $scope.profit = rsp.data.Result.Balance;
            } else {
                $scope.messeger = rsp.data.Msg;
            }
        });
    }
});
app.controller('PHViewModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $http, $rootScope, $state, $stateParams, messeger, title, params, params2, commonFactory) {
    $scope.messeger = messeger,
    $scope.title = title,
    $scope.processing = false,
    $scope.AlertType = "warning";
    $scope.user = {},
    $scope.parent = {},
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    },
    GetUserByID(params);
    GetParentByID(params);
    GetConfirmTransferPH(params2);
    function GetUserByID(userId) {
        commonFactory.getUserById(userId).then(function (rsp) {
            $scope.user = rsp.data;

        }, function (error) {
            $scope.status = error.message;
        });
    };
    function GetParentByID(userId) {
        commonFactory.getParentById(userId).then(function (rsp) {
            if (rsp.data.Status) {
                $scope.parent = rsp.data.Result;
            }

        }, function (error) {
            $scope.status = error.message;
        });
    };
    function GetConfirmTransferPH(transId) {
        commonFactory.getConfirmTransferPH(transId).then(function (rsp) {
            if (rsp.data.Status) {
                if (rsp.data.Result != null) {
                    $scope.imagePH = $rootScope.API + rsp.data.Result.Image;
                }
            }

        }, function (error) {
            $scope.status = error.message;
        });
    };
});